package com.unknown.dapp.uni.swap.log.job;


import com.unknown.dapp.uni.swap.log.service.ITokenSwapRecordService;
import lombok.extern.slf4j.Slf4j;
import org.cloud.core.redis.RedisUtil;
import org.cloud.scheduler.constants.MisfireEnum;
import org.cloud.scheduler.job.BaseQuartzJobBean;
import org.jetbrains.annotations.NotNull;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * ERC20和ERC721通证转账处理任务
 */
@Slf4j
public class TokenSwapJob extends BaseQuartzJobBean {

    @Autowired
    private ITokenSwapRecordService contractLogsService;


    @Autowired
    private RedisUtil redisUtil;

    @Override
    protected void init() {
        this.jobName = "pair swap交易处理处理任务";
        jobData.put("description", "pair swap交易处理处理任务");
        this.jobTime = "0/2 * * * * ?";
        setMisfire(MisfireEnum.CronScheduleMisfireEnum.MISFIRE_INSTRUCTION_DO_NOTHING);
    }

    @Override
    protected void executeInternal(@NotNull JobExecutionContext jobExecutionContext) throws JobExecutionException {

        final String redisLockerName = "TokenSwapJob.locker";
        if (!redisUtil.getLock(redisLockerName, 360 * 60 * 1000)) {
            return;
        }
        try {
            contractLogsService.processSwapTokenTransferLogs();
        } finally {
            redisUtil.releaseLock(redisLockerName);
        }


    }
}